package com.bestcem.xm.survey.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.survey.entity.SurveyQuotaCondition;
import com.bestcem.xm.survey.service.SurveyQuotaConditionService;
import com.bestcem.xm.survey.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Slf4j
@Api(tags = "配额")
@RestController
@RequiredArgsConstructor
@RequestMapping("/survey/")
public class SurveyQuotaController {
    private @NonNull SurveyQuotaConditionService surveyQuotaConditionService;

    @ApiOperation(value = "获取普通配额")
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions",
            "/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Object> getQuota(@ApiIgnore Page<SurveyQuotaCondition> page,
                                         @PathVariable(value = "projectId") String projectId,
                                         @PathVariable(value = "quotaId") String quotaId,
                                         @PathVariable(value = "id", required = false) String id,
                                         HttpServletRequest request) {
        if (Objects.nonNull(id)) {
            return XmResultJson.success(surveyQuotaConditionService.getById(id));
        }
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(surveyQuotaConditionService.getAll(projectId, quotaId));
        }
        return XmResultJson.success(surveyQuotaConditionService.getByPage(page, projectId, quotaId));
    }

    @ApiOperation(value = "获取用于编辑问卷校验配额")
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/options"})
    public XmResultJson<JSONObject> getQuotaCheck(@PathVariable(value = "projectId") String projectId) {
        return XmResultJson.success(surveyQuotaConditionService.getNormalQuotaUsedOptions(projectId));
    }

    @ApiOperation(value = "创建普通配额")
    @PostMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions"})
    public XmResultJson<JSONObject> createQuota(@PathVariable(value = "projectId") String projectId,
                                            @PathVariable(value = "quotaId") String quotaId,
                                            @RequestBody Object param) {
        return XmResultJson.success(surveyQuotaConditionService.createQuota(projectId, quotaId, param));
    }

    @ApiOperation(value = "修改普通配额")
    @PutMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions",
        "/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<JSONObject> updateQuota(@PathVariable(value = "projectId") String projectId,
                                                @PathVariable(value = "quotaId") String quotaId,
                                                @PathVariable(value = "id", required = false) String id,
                                                @RequestBody JSONObject param) {
        return XmResultJson.success(surveyQuotaConditionService.updateQuota(projectId, quotaId, id, param));
    }

    @ApiOperation(value = "修改普通配额数量")
    @PutMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/normal/quotacheckitems"})
    public XmResultJson<JSONObject> updateNormalQuota(@PathVariable(value = "projectId") String projectId,
                                                @PathVariable(value = "quotaId") String quotaId,
                                                @RequestBody JSONObject param) {
        return XmResultJson.success(surveyQuotaConditionService.updateNormalQuota(projectId, quotaId, param));
    }

    @ApiOperation(value = "删除配额条件")
    @DeleteMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions",
        "/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<JSONObject> deleteQuota(@PathVariable(value = "projectId") String projectId,
                                                @PathVariable(value = "quotaId") String quotaId,
                                                @PathVariable(value = "id", required = false) String id,
                                                @RequestBody JSONObject param) {
        return XmResultJson.success(surveyQuotaConditionService.deleteQuota(projectId, quotaId, id, param));
    }

    @ApiOperation(value = "更新配额检查项")
    @PutMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/{quotaCondId:[a-fA-F0-9]{24}}/quotacheckitems/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<JSONObject> updateQuotaItem(@PathVariable(value = "projectId") String projectId,
                                                @PathVariable(value = "quotaId") String quotaId,
                                                @PathVariable(value = "quotaCondId") String quotaCondId,
                                                @PathVariable(value = "id", required = false) String id,
                                                @RequestBody JSONObject param) {
        return XmResultJson.success(surveyQuotaConditionService.updateQuotaItem(projectId, quotaId, quotaCondId, id, param));
    }

    @ApiOperation(value = "交叉配额设置配额返回值页面")
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/cross/quotacheckitems"})
    public XmResultJson<JSONObject> getQuotaCross(@PathVariable(value = "projectId") String projectId,
                                                  @PathVariable(value = "quotaId") String quotaId) {
        return XmResultJson.success(surveyQuotaConditionService.getQuotaCross(projectId, quotaId));
    }

    @ApiOperation(value = "更新配额检查项")
    @PutMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/cross/quotacheckitems/{id:[a-fA-F0-9]{24}}",
        "/projects/{projectId:[a-fA-F0-9]{24}}/quotas/{quotaId:[a-fA-F0-9]{24}}/quotaconditions/cross/quotacheckitems"})
    public XmResultJson<JSONObject> updateQuotaCrossItem(@PathVariable(value = "projectId") String projectId,
                                                    @PathVariable(value = "quotaId") String quotaId,
                                                    @PathVariable(value = "id", required = false) String id,
                                                    @RequestBody JSONObject param) {
        return XmResultJson.success(surveyQuotaConditionService.updateQuotaCrossItem(projectId, quotaId, id, param));
    }

    @ApiOperation(value = "用于编辑问卷校验配额")
    @GetMapping(value = {"/projects/{projectId:[a-fA-F0-9]{24}}/quotas/qstruct"})
    public XmResultJson<JSONObject> getQuotaStruct(@PathVariable(value = "projectId") String projectId) {
        return XmResultJson.success(surveyQuotaConditionService.getQuotaStruct(projectId));
    }
}